<?php
class CanbosController extends AppController {

	var $name = 'Canbos';
	var $uses = array('Canbo','Tgiachidoancb','Chidoansv');
	
	function index() {
		$this->Canbo->recursive = 0;
        $this->set('canbos', $this->paginate());
        $phuongxas = $this->Canbo->Phuongxa->find('list',array('fields'=>'Phuongxa.tenphuongxa'));
		$tongiaos = $this->Canbo->Tongiao->find('list',array('fields'=>'Tongiao.tentongiao'));
		$dantocs = $this->Canbo->Dantoc->find('list',array('fields'=>'Dantoc.tendantoc'));
		$this->set(compact('phuongxas', 'tongiaos', 'dantocs'));
	}
    function search(){
        $this->layout='search';
        $column = array('Canbo.id',
                        'Canbo.mscb',
                        'Canbo.phuongxa_id',
                        'Canbo.tongiao_id',
                        'Canbo.dantoc_id',
                        'Canbo.hoten',
                        'Canbo.noisinh',
                        'Canbo.phai',
                        'Canbo.diachi',
                        'Canbo.sodienthoai',
                        'Canbo.email',
                        'Canbo.namvaonghe',
                        'Canbo.noivaodoan',
                        'Canbo.noivaodang',
                        'Canbo.ngayttdoan',
                        'Canbo.ngaysinh',
                        'Canbo.ngayvaodoan',
                        'Canbo.ngayvaodang'
                        );
        $value = $this->params['pass'];
        //pr($value);exit;
        //$conditions[]=array();
        $ii = 15;
        for($i=0;$i<count($column);$i++){
            if($value[$i]!="null"){
                if($i<15){
                    if($i==2||$i==3||$i==4){
                        $conditions[] = array($column[$i]=>$value[$i]);
                    }else{
                        $conditions[] = array($column[$i].' Like'=>'%'.$value[$i].'%');    
                    }
                       
                
                }else{
                    if($value[$ii]!="null"){
                        $conditions[] = array($column[$i].">="."'".$value[$ii++]."'",$column[$i]."<="."'".$value[$ii++]."'");    
                    }
                    
                }
            }
        }
        //pr($conditions);
        if(!empty($conditions)){
            $results=$this->Canbo->find('all',array('conditions'=>$conditions));    
        }else $results=$this->Canbo->find('all');
        
        $this->set('results', $results);  
    }
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid canbo', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('canbo', $this->Canbo->read(null, $id));
	}

	function add() {
		if (!empty($this->data))
		{
			$this->data['Canbo']['ngaysinh']=date('Y-m-d',strtotime($this->data['Canbo']['ngaysinh']));
			if(!empty($this->data['Canbo']['ngayvaodoan'])&& $this->data['Canbo']['ngayvaodoan']!='0000-00-00')
				$this->data['Canbo']['ngayvaodoan']=date('Y-m-d',strtotime($this->data['Canbo']['ngayvaodoan']));
			if(!empty($this->data['Canbo']['ngayvaodang'])&& $this->data['Canbo']['ngayvaodang']!='0000-00-00')
				$this->data['Canbo']['ngayvaodang']=date('Y-m-d',strtotime($this->data['Canbo']['ngayvaodang']));
			if(!empty($this->data['Canbo']['ngayttdoan'])&& $this->data['Canbo']['ngayttdoan']!='0000-00-00')
				$this->data['Canbo']['ngayttdoan']=date('Y-m-d',strtotime($this->data['Canbo']['ngayttdoan']));				
			if ($this->Canbo->save($this->data))
			{
				
				$chidoansv_id = $this->data['Canbo']['chidoansv_id'];
				$record['Tgiachidoancb']['canbo_id']=$this->Canbo->id;
				$record['Tgiachidoancb']['chidoansv_id']=$chidoansv_id;
//				/$record['Tgiachidoancb']['ngayvao']=date('Y-m-d');
				$record['Tgiachidoancb']['ngaychuyen']=NULL;
				if($this->Tgiachidoancb->save($record))
				$this->Session->setFlash(__('Đã Lưu thông tin cán bộ', true));
				$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan','cb',$chidoansv_id));
			}
			else
			{
				$this->Session->setFlash(__('Lỗi khi lưu. Vui lòng thử lại', true));
			}
		}			
		$chidoansvs = $this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>NULL,'khoa_id'=>$this->Session->read('khoa_id')),'fields'=>array('tenchidoan')));
		
		$tinhtps =$this->Canbo->Phuongxa->Quanhuyen->Tinhtp->find('list',array('fields'=>array('tentinhtp')));
		$tongiaos = $this->Canbo->Tongiao->find('list',array('fields'=>array('tentongiao')));
		$dantocs = $this->Canbo->Dantoc->find('list',array('fields'=>array('tendantoc')));
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs','quanhuyen','tinhtp','quanhuyens','tinhtps','chidoansvs','tgiachidoancb'));
	}
	

	function edit($id = null) 
	{
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
		}
		if (!empty($this->data))
		{
			$this->data['Canbo']['ngaysinh']=date('Y-m-d',strtotime($this->data['Canbo']['ngaysinh']));
			if(!empty($this->data['Canbo']['ngayvaodoan'])&& $this->data['Canbo']['ngayvaodoan']!='0000-00-00')
				$this->data['Canbo']['ngayvaodoan']=date('Y-m-d',strtotime($this->data['Canbo']['ngayvaodoan']));
			if(!empty($this->data['Canbo']['ngayvaodang'])&& $this->data['Canbo']['ngayvaodang']!='0000-00-00')
				$this->data['Canbo']['ngayvaodang']=date('Y-m-d',strtotime($this->data['Canbo']['ngayvaodang']));
			if(!empty($this->data['Canbo']['ngayttdoan'])&& $this->data['Canbo']['ngayttdoan']!='0000-00-00')
				$this->data['Canbo']['ngayttdoan']=date('Y-m-d',strtotime($this->data['Canbo']['ngayttdoan']));				
			if ($this->Canbo->save($this->data)) {
				$canbo_id = $id;
				$chidoansv_id = $this->data['Canbo']['chidoansv_id'];
				$tgiacd = $this->Tgiachidoancb->find('first',array('conditions'=>array('chidoansv_id'=>$chidoansv_id,'canbo_id'=>$canbo_id,'ngaychuyen'=>null)));
				if(empty($tgiacd))
				{
					if($this->Tgiachidoancb->updateAll(array('ngaychuyen'=>'"'.date('Y-m-d').'"'),array('canbo_id'=>$id)))
					{
						$record['Tgiachidoancb']['canbo_id']=$id;
						$record['Tgiachidoancb']['chidoansv_id']=$chidoansv_id;
						$record['Tgiachidoancb']['ngayvao']=date('Y-m-d');
						$record['Tgiachidoancb']['ngaychuyen']=NULL;
						$this->Tgiachidoancb->save($record);
					}
				}
				$this->Session->setFlash(__('Đã sửa thông tin cán bộ', true));
				$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
			} else {
				$this->Session->setFlash(__('Lỗi khi lưu. Vui lòng thử lại', true));
			}
		}
		if (empty($this->data))
		{			
			$this->data = $this->Canbo->read(null, $id);
			$this->data['Canbo']['ngaysinh']=date('d-m-Y',strtotime($this->data['Canbo']['ngaysinh']));
			if(!empty($this->data['Canbo']['ngayvaodoan'])&& $this->data['Canbo']['ngayvaodoan']!='0000-00-00')
				$this->data['Canbo']['ngayvaodoan']=date('d-m-Y',strtotime($this->data['Canbo']['ngayvaodoan']));
			if(!empty($this->data['Canbo']['ngayvaodang'])&& $this->data['Canbo']['ngayvaodang']!='0000-00-00')
				$this->data['Canbo']['ngayvaodang']=date('d-m-Y',strtotime($this->data['Canbo']['ngayvaodang']));
			if(!empty($this->data['Canbo']['ngayttdoan'])&& $this->data['Canbo']['ngayttdoan']!='0000-00-00')
				$this->data['Canbo']['ngayttdoan']=date('d-m-Y',strtotime($this->data['Canbo']['ngayttdoan']));
			$quanhuyen =$this->Canbo->Phuongxa->find('first',array('conditions'=>array('Phuongxa.id'=>$this->data['Canbo']['phuongxa_id']),'fields'=>array('quanhuyen_id')));
			$tinhtp =$this->Canbo->Phuongxa->Quanhuyen->find('first',array('conditions'=>array('Quanhuyen.id'=>$quanhuyen['Phuongxa']['quanhuyen_id']),'fields'=>array('tinhtp_id')));
		}
		
		$tgiachidoancb = $this->Tgiachidoancb->find('first',array('conditions'=>array('khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen'=>null,'canbo_id'=>$id)));
		$chidoansvs = $this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>NULL,'khoa_id'=>$this->Session->read('khoa_id')),'fields'=>array('tenchidoan')));
		
		$tinhtps =$this->Canbo->Phuongxa->Quanhuyen->Tinhtp->find('list',array('fields'=>array('tentinhtp')));
		$quanhuyens =$this->Canbo->Phuongxa->Quanhuyen->find('list',array('conditions'=>array('tinhtp_id'=>$tinhtp['Quanhuyen']['tinhtp_id']),'fields'=>array('tenquanhuyen')));
		$phuongxas = $this->Canbo->Phuongxa->find('list',array('conditions'=>array('quanhuyen_id'=>$quanhuyen['Phuongxa']['quanhuyen_id']),'fields'=>array('tenphuongxa')));
		$tongiaos = $this->Canbo->Tongiao->find('list',array('fields'=>array('tentongiao')));
		$dantocs = $this->Canbo->Dantoc->find('list',array('fields'=>array('tendantoc')));
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs','quanhuyen','tinhtp','quanhuyens','tinhtps','chidoansvs','tgiachidoancb'));
		
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for canbo', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Canbo->delete($id)) {
			$this->Session->setFlash(__('Canbo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Canbo was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function updatequanhuyen()
	{
		$this->layout='ajax';
		$tinhtp_id =$this->data['Canbo']['tinhtp_id']; 
		$quanhuyens =$this->Canbo->Phuongxa->Quanhuyen->find('list',array('conditions'=>array('tinhtp_id'=>$tinhtp_id),'fields'=>array('tenquanhuyen')));
		$this->set(compact('quanhuyens')); 
	}
	
	function updatephuongxa()
	{
		$this->layout='ajax';
		$quanhuyen_id =$this->data['Canbo']['quanhuyen_id']; 
		$phuongxas =$this->Canbo->Phuongxa->find('list',array('conditions'=>array('quanhuyen_id'=>$quanhuyen_id),'fields'=>array('tenphuongxa')));
		$this->set(compact('phuongxas'));
	}
	
	function updateempty()
	{
		$this->layout='ajax';
	}
	
}
